#!/usr/bin/python
# -*- coding: UTF-8 -*-

from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.test_logic.FileTransport import sftp_stream_copy
from datetime import datetime
import time


class IPMCSET_NTP_001(Case):
    """
            Name:
                IPMCSET_NTP_001
            Label:
                设置NTP状态
            PreCondition:
                BMC正常启动
            TestStep:
                1) 命令行执行 ipmcset -t ntp -d status -v enabled ，ipmcget -d ntpinfo
            ExpectedResult:
                A) ntp使能打开，无异常报错，命令生效。
            Parameters:

            Author:
                baixin liuya
            Last Modified:

            Remark:

        """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 查询NTP信息")
        resp = self.bmc_host.run({"command": ["ipmcget -d ntpinfo"], "timeout": 10})
        self.logger.info(resp)
        ntp_status = resp['stdout'].split("Preferred Server")[0]
        if "disabled" in ntp_status:
            ntp_status = "disabled"
        else:
            ntp_status = "enabled"

        self.log_step("Step2: 设置NTP 为enabled")
        resp = self.bmc_host.run({"command": ["ipmcset -t ntp -d status -v enabled"], "timeout": 10})
        if resp['rc'] != 0:
            self.assertIsNotNone(None, f"{resp['stderr']}")
        else:
            self.logger.info(resp['stdout'])

        self.log_step("Step3: 设置NTP 为disabled")
        resp = self.bmc_host.run({"command": ["ipmcset -t ntp -d status -v disabled"], "timeout": 10})
        if resp['rc'] != 0:
            self.assertIsNotNone(None, f"{resp['stderr']}")
        else:
            self.logger.info(resp['stdout'])

        self.log_step("Step4: 恢复NTP为初始状态")
        resp = self.bmc_host.run({f"command": [f"ipmcset -t ntp -d status -v {ntp_status}"], "timeout": 10})
        if resp['rc'] != 0:
            self.assertIsNotNone(None, f"{resp['stderr']}")
        else:
            self.logger.info(resp['stdout'])

    # 测试结束后恢复环境
    def post_test_case(self):
        pass